/**
 * Rest (PostgREST) 模块 API
 * PostgREST 配置相关接口
 */

import { request, type ApiResponse } from "../request";

// ============ 类型定义 ============

/**
 * PostgREST 配置（含 JWT 密钥）
 */
export interface PostgrestConfigWithJWTSecret {
	db_schema: string;
	max_rows: number;
	db_extra_search_path: string;
	db_pool: number | null;
	jwt_secret?: string;
}

/**
 * PostgREST 配置
 */
export interface PostgrestConfig {
	db_schema: string;
	max_rows: number;
	db_extra_search_path: string;
	db_pool: number | null;
}

/**
 * 更新 PostgREST 配置请求
 */
export interface UpdatePostgrestConfigBody {
	db_extra_search_path?: string;
	db_schema?: string;
	max_rows?: number;
	db_pool?: number;
}

// ============ API 方法 ============

/**
 * 获取项目 PostgREST 配置
 */
export function getPostgrestConfig(
	ref: string
): Promise<ApiResponse<PostgrestConfigWithJWTSecret>> {
	return request.get<PostgrestConfigWithJWTSecret>(`/v1/projects/${ref}/postgrest`);
}

/**
 * 更新项目 PostgREST 配置
 */
export function updatePostgrestConfig(
	ref: string,
	data: UpdatePostgrestConfigBody
): Promise<ApiResponse<PostgrestConfig>> {
	return request.patch<PostgrestConfig>(
		`/v1/projects/${ref}/postgrest`,
		data as Record<string, unknown>
	);
}

export default {
	getPostgrestConfig,
	updatePostgrestConfig
};
